Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to avoid segfault with uncompressed bam #1632

Merged
merged 2 commits into from
Jun 9, 2023

Conversation

vasudeva8
Copy link
Contributor

Fix for #1617
when bam write is attempted with 'u', the bgzf compression/decompression buffers are not allocated as it denotes uncompressed output. bgzf_flush, called at end, accesses the compression buffers and causes segfault in such case.

Binary data files are implicitly expected to be bgzf compressed and this 'u' flag is not valid / supported.

The fix made is to remove the 'u' flag on write of bam/bcf that the output is written as bgzf compressed with default compression level.

hts.c Outdated
@@ -866,6 +871,11 @@ htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt)
*mode_c = format_to_mode[fmt->format];
}

// Uncompressed bam/bcf is not supported, remove 'u' flag on write
if (uncomp && *mode_c == 'b' && (strchr(smode, 'w') || strchr(smode, 'a'))) {
memcpy(uncomp, uncomp + 1, cp2 - uncomp - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use memmove() here because the source and destination buffers overlap, and memcpy() disallows that. But better still would be to turn the 'u' into a '0', as the result will then be uncompressed while still BGZF wrapped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly.

@daviesrob daviesrob self-assigned this Jun 6, 2023
@daviesrob daviesrob merged commit 5e0ccef into samtools:develop Jun 9, 2023
@vasudeva8 vasudeva8 deleted the segfault branch August 1, 2023 12:15
vasudeva8 added a commit to vasudeva8/htslib that referenced this pull request Aug 17, 2023
Avoids segfault when writing bam/bcf with mode "wbu" by
changing "wbu" to "wb0".  The ensures the output file will
be properly wrapped in BGZF blocks, even though it's not
been compressed.

Fixes samtools#1617
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants